package com.zingrow.common.filter;

import com.idaycrm.idaybase.utils.JsonUtils;
import com.zingrow.common.ReturnStandardDataFormat;
import com.zingrow.web.base.CodeMsg;

import java.io.IOException;
import java.io.OutputStreamWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;

public class SessionFilter
  implements Filter
{
  public void destroy() {}
  
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException
  {
    HttpServletRequest httpRequest = (HttpServletRequest)request;
    HttpServletResponse httpResponse = (HttpServletResponse)response;
    httpResponse.setHeader("Content-type", "text/html;charset=utf-8"); 
    if (request.getAttribute("allow_tag") != null)
    {
      chain.doFilter(request, response);
      return;
    }
    HttpSession httpSession = httpRequest.getSession();
    
//    System.out.println("---------" + httpSession.getId());
//    String userAgent1 = httpRequest.getHeader("Cookie");
//    System.out.println(userAgent1.toString());
    String userAgent = httpRequest.getHeader("User-Agent");
    

    String ajax = httpRequest.getHeader("X-Requested-With");
    if (httpSession.getAttribute("userinfo") == null)
    {
      
//      if( httpRequest.getSession(false) == null){
//          ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(Integer.valueOf(10001), "操作失败", null);
//          
//
//          standardData.setReturnCode(Integer.valueOf(99994));
//          standardData.setReturnMessage("Session已过期");
//          httpResponse.getWriter().write(JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss"));
//      }
      if (StringUtils.isNotBlank(ajax))
      {
        httpRequest.getRequestDispatcher("/user/sessionTimeoutWeb").forward(httpRequest, httpResponse);
      }
      else if (("ios".equals(userAgent)) || ("android".equals(userAgent)))
      {
          
				ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(Integer.valueOf(10001), "操作失败",
						null);
				standardData.setReturnCode(Integer.valueOf(99994));
				standardData.setReturnMessage(CodeMsg.MSG_LOGIN_EXPIRE);
				httpResponse.setCharacterEncoding("UTF-8");
				OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpResponse.getOutputStream(), "utf-8");
				outputStreamWriter.write(JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss"));
				outputStreamWriter.flush();
				outputStreamWriter.close();
				
      }
      else 
      {
       // String path = httpRequest.getScheme() + "://" + httpRequest.getServerName() + ":" + httpRequest.getServerPort() + httpRequest.getContextPath() + "/";
        ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(Integer.valueOf(10001),"操作失败",null);
        standardData.setReturnCode(Integer.valueOf(10006));
        standardData.setReturnMessage(CodeMsg.MSG_LOGIN_ERROR);
        httpResponse.getWriter().write(JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss"));
        httpRequest.getRequestDispatcher("/login/login.jsp").forward(request, httpResponse);
      }
    }
    
    else if(httpSession.getAttribute("isReplace")!=null && "1".equalsIgnoreCase((httpSession.getAttribute("isReplace").toString())))
    {
        if (("ios".equals(userAgent)) || ("android".equals(userAgent)))
        {
            ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(Integer.valueOf(10001), "操作失败", null);
            standardData.setReturnCode(Integer.valueOf(10006));
            httpResponse.setCharacterEncoding("UTF-8");
            standardData.setReturnMessage(CodeMsg.MSG_LOGIN_ERROR);
            httpSession.removeAttribute("isReplace");
            httpResponse.getWriter().write(JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss"));
        }
    }
    else if(httpSession.getAttribute("isRemove")!=null && "1".equalsIgnoreCase((httpSession.getAttribute("isRemove").toString())))
    {
    	if (("ios".equals(userAgent)) || ("android".equals(userAgent)))
    	{
    		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(Integer.valueOf(10001), "操作失败", null);
    		standardData.setReturnCode(Integer.valueOf(10007));
    		httpResponse.setCharacterEncoding("UTF-8");
    		standardData.setReturnMessage("您的账号已被删除！");
//    		httpSession.removeAttribute("isRemove");
    		httpResponse.getWriter().write(JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss"));
    	}
    }
    else
    {
      chain.doFilter(httpRequest, httpResponse);
    }
  }
  
  public void init(FilterConfig arg0)
    throws ServletException
  {}
}



/* Location:           F:\项目\HandleAdmin-1.3.0.4.zip

 * Qualified Name:     WEB-INF.classes.com.zingrow.common.filter.SessionFilter

 * JD-Core Version:    0.7.0.1

 */